package com.zhkj.shixi.tvplayer.VideoPlayer;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.media.TimedText;
import android.util.Log;
import android.view.SurfaceHolder;
import android.widget.ImageView;

import com.zhkj.shixi.tvplayer.Interface.IBottomControlBar;
import com.zhkj.shixi.tvplayer.MyMediaPlayer;
import com.zhkj.shixi.tvplayer.Interface.PlayerInfo;
import com.zhkj.shixi.tvplayer.PicturePlayer.CommonDialog;
import com.zhkj.shixi.tvplayer.R;


import java.io.File;
import java.io.IOException;
import java.util.List;

public class BottomControlBar implements IBottomControlBar {
    private VideoPlayerActivity videoPlayerActivity;
    final String TAG="BottomControlBar";
    private PlayerInfo playerInfo;
    public BottomControlBar(VideoPlayerActivity videoPlayerActivity,PlayerInfo playerInfo){
        this.videoPlayerActivity =videoPlayerActivity;
        this.playerInfo=playerInfo;
    }
    public void play(MyMediaPlayer mediaPlayer){
        mediaPlayer.play();
    }
    public void pause(MyMediaPlayer mediaPlayer){
        mediaPlayer.pause();
    }

    public void fastGo(MyMediaPlayer mediaPlayer){
        mediaPlayer.setTime(mediaPlayer.getCurrentPosition()+1000);
    }

    public void fastBack(MyMediaPlayer mediaPlayer){
        mediaPlayer.setTime(mediaPlayer.getCurrentPosition()-1000);
    }

    public void next(MyMediaPlayer mediaPlayer, SurfaceHolder holder){
        List<File> files = playerInfo.getPlayList();
        if(files.indexOf(playerInfo.getPlayingFile())+1<files.size()) {
//            try {
//                mediaPlayer.reset();
//                mediaPlayer.setDataSource(files.get(files.indexOf(playerInfo.getPlayingFile()) + 1).getAbsolutePath());
//                mediaPlayer.play();
//            }catch (IOException e){
//                e.printStackTrace();
//            }
            playerInfo.setPlayingFile(files.get(files.indexOf(playerInfo.getPlayingFile()) + 1));
            mediaPlayer.init(files.get(files.indexOf(playerInfo.getPlayingFile())).getAbsolutePath(),holder);
            mediaPlayer.play();
        }else{
            CommonDialog.Builder builder =new CommonDialog.Builder(videoPlayerActivity);
            builder.setMessage("已经是最后一个文件了");
            builder.setTitle("提示");
            builder.setCenterTextView("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }
    }


    public void prev(MyMediaPlayer mediaPlayer, SurfaceHolder holder){
        List<File> files = playerInfo.getPlayList();
        if(files.indexOf(playerInfo.getPlayingFile())-1>=0){
//            try {
//                mediaPlayer.reset();
//                mediaPlayer.setDataSource(files.get(files.indexOf(playerInfo.getPlayingFile()) - 1).getAbsolutePath());
//                mediaPlayer.play();
//            }catch (IOException e){
//                e.printStackTrace();
//            }
            playerInfo.setPlayingFile(files.get(files.indexOf(playerInfo.getPlayingFile()) - 1));
            mediaPlayer.init(files.get(files.indexOf(playerInfo.getPlayingFile())).getAbsolutePath(),holder);
            mediaPlayer.play();
        }
        else {
            CommonDialog.Builder builder =new CommonDialog.Builder(videoPlayerActivity);
            builder.setMessage("没有上一个文件");
            builder.setTitle("提示");
            builder.setCenterTextView("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }
    }

    public void miniMap(Activity activity){
        MediaMetadataRetriever media;
        List<File> files = playerInfo.getPlayList();
        ImageView imgNext = activity.findViewById(R.id.imgNext);
        ImageView imgPrev = activity.findViewById(R.id.imgPrev);
        if(files.indexOf(playerInfo.getPlayingFile())+1<files.size()) {
            media = new MediaMetadataRetriever();
            media.setDataSource(files.get(files.indexOf(playerInfo.getPlayingFile())+1).getAbsolutePath());
            Bitmap bpNext = media.getFrameAtTime();
            imgNext.setImageBitmap(bpNext);
        }
        else {
            imgNext.setImageDrawable(activity.getResources().getDrawable(R.drawable.player_video_img_dwon));
        }
        if(files.indexOf(playerInfo.getPlayingFile())-1>=0){
            media = new MediaMetadataRetriever();
            media.setDataSource(files.get(files.indexOf(playerInfo.getPlayingFile())-1).getAbsolutePath());
            Bitmap bpPrev = media.getFrameAtTime();
            imgPrev.setImageBitmap(bpPrev);
        }
        else {
            imgPrev.setImageDrawable(activity.getResources().getDrawable(R.drawable.player_video_img_dwon));
        }
    }

    public void loadSubtittle(File file, Activity activity, MyMediaPlayer mediaPlayer) {
        //TODO 获取选定的字幕文件，绘制在传入的Activity上
        try {
            mediaPlayer.addTimedTextSource(file.getAbsolutePath(), MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);//srt
        }catch (IOException e){
            e.printStackTrace();
        }

        MediaPlayer.TrackInfo[] trackInfos = mediaPlayer.getTrackInfo();

        if (trackInfos != null && trackInfos.length > 0) {
            for (int i = 0; i < trackInfos.length; i++) {
                final MediaPlayer.TrackInfo info = trackInfos[i];

                Log.w(TAG, "TrackInfo: " + info.getTrackType() + " "
                        + info.getLanguage());

                if (info.getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO) {
                    // mMediaPlayer.selectTrack(i);
                } else if (info.getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) {
                    mediaPlayer.selectTrack(i);
                }
            }
        }
        mediaPlayer.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() {
            @Override
            public void onTimedText(MediaPlayer mp, TimedText text) {
                if (text != null) {
                    Log.d(TAG, "text = " + text.getText());
                    //TODO 绘制字幕
                }
            }
        });
    }

    public void refreshByProcessBar(MyMediaPlayer mediaPlayer,int time){
       mediaPlayer.setTime(time);
    }
}
